/**
 * @author hugh
 * @time  2021-01-10 11:40
 * @title  vuex配置
 * @desc
 *
 */
import { createStore } from 'vuex'
//其他模块
import other  from './modules/other';
 const baseMenu: any = {
        checked: false,
        childs: [],
        css: "menu",
        hidden: false,
        menu: false,
        menuId: 0,
        name: "首页",
        parentId: 0,
        id: '',
        path: "/statistics",
        sort: 1,
        url: "",
        open: false
      }
export default createStore({
  state: {
    clients:{
      additionalInformation: {},
      expire: 0,
      refreshToken: "",
      scope: [""],
      token: "",
      appId:'',
      appKey:'',
      tokenType: "bearer",
      user:{
        avatar: require('../assets/avatar/avatar1.jpeg'),
        username:'游客'
      }
    },
    refreshTokenTime:0,
    roles:[],
    appLists:[],
    clientId:process.env.VUE_APP_BASE_CLIENT_ID,
    fromData:{},
    tableHeight:document.documentElement.clientHeight*0.65,
    conentHeight:document.documentElement.clientHeight-60,
    isPhone:document.documentElement.clientWidth<document.documentElement.clientHeight,
    menu:baseMenu,
    menuslist: [baseMenu],
    editableTabs: [baseMenu],
    headers: [baseMenu],
    menuCheck:[baseMenu.menuId],
    editableTabsValue: '0',
    tabSelectindex: 1,
    menuActive: ''

  },
  mutations: {
    /**
     * start: 储存全局状态
     * */
    setData(state, params) {
    	state = Object.assign(state, params);
    },
    setMenusList(state:any,data:any){
      state.menuslist=data;
    },
    setEditableTabs(state:any,data:any){
      state.editableTabs=data;
    },
    setHeaders(state:any,data:any){
      state.headers=data;
    },
    setMenuCheck(state:any,data:any){
      state.menuCheck=data;
    },
    setEditableTabsValue(state:any,data:any){
      state.editableTabsValue=data;
    },
    setTabSelectindex(state:any,data:any){
      state.tabSelectindex=data;
    },
    setMenuActive(state:any,data:any){
      state.menuActive=data;
    },
    resetDevice(state:any){
      state.isPhone=document.documentElement.clientWidth<document.documentElement.clientHeight;
    },
    /**
     * 登录并保存客户端信息
     */
    login(state:any,data:any){
      state.clients=data;
      state.refreshTokenTime=new Date().getTime();
      localStorage.setItem('refreshTokenTime', state.refreshTokenTime);
      localStorage.setItem('token', data.token);
      localStorage.setItem('refreshToken', data.refreshToken);
      localStorage.setItem('scope', data.scope);
      localStorage.setItem('clients',JSON.stringify(data));
    },
    getClients(state:any){
      let clients:any=localStorage.getItem('clients');
      if(!clients){
        return;
      }
      clients=JSON.parse(clients);
      if(clients.user){
        state.clients=clients;
      }
       let refreshTokenTime:any=localStorage.getItem('refreshTokenTime');
        state.refreshTokenTime=refreshTokenTime;
    },
    loginOut(state:any){
      localStorage.removeItem('clients');
      localStorage.removeItem('token');
      localStorage.removeItem('refreshToken');
      localStorage.removeItem('scope');
      state.clients={
        additionalInformation: {},
        expire: 0,
        refreshToken: "",
        scope: [""],
        token: "",
        tokenType: "bearer",
        user:{
          avatar: require('../assets/avatar/avatar1.jpeg'),
          username:'游客'
        }
      };
    },
    /**
     * 保存用户信息
     */
    setFromData(state:any,data:any){
      state.fromData=data;
    },
    /**
     * 保存用户信息
     */
    setUser(state:any,data:any){
      state.user=data;
    },
    /**
     * 保存客户端ID
     */
    setClientId(state:any,data:number){
      state.clientId=data;
    },/**
     * 保存权限
     */
    setRoles(state:any,data:number){
      state.roles=data;
    },
    /**
     * 保存应用列表
     */
    setAppLists(state:any,data:number){
      state.appLists=data;
    },
    setParams(state:any,menu:any){
      state.menuActive=menu.id;
      state.editableTabsValue=menu.menuId.toString();
      if (state.menuCheck.indexOf(menu.menuId) === -1) {
        state.editableTabs.push(menu);
        state.menuCheck.push(menu.menuId);
      }
      console.log(state)
    }
  },
  actions: {
    navigateTo(context:any,path:string){
      let tableData=context.state.menuslist;
      let key='path';
      let headers:any=[];
      //debugger
      for (let i = 0; i < tableData.length; i++) {
          if(path.indexOf(tableData[i][key])!==-1){
             context.commit('setParams',tableData[i]);
             headers.push(tableData[i]);
             context.commit('setHeaders',headers);
            return;
          }
            if(tableData[i].childs){
              for (let j = 0,j_length=tableData[i].childs.length; j < j_length; j++) {
                if(path.indexOf(tableData[i].childs[j][key])!==-1){
                   context.commit('setParams',tableData[i].childs[j]);
                   headers.push(tableData[i]);
                   headers.push(tableData[i].childs[j]);
                   context.commit('setHeaders',headers);
                   return;
                }
                if(tableData[i].childs[j].childs){
                  for (let k = 0,k_length=tableData[i].childs[j].childs.length; k <k_length ; k++) {
                    if(path.indexOf(tableData[i].childs[j].childs[k][key])!==-1){
                        context.commit('setParams',tableData[i].childs[j].childs[k]);
                        headers.push(tableData[i]);
                        headers.push(tableData[i].childs[j]);
                        headers.push(tableData[i].childs[j].childs[k]);
                        context.commit('setHeaders',headers);
                       return;
                    }
                  }
                }
              }
            }
        }

    }
  },
  modules: {
    other

  }
})
